import App from "./App";
import "uno.css";

// 引入语言包，注意路径
import zh from "./common/locales/zh.js";
import en from "./common/locales/en.js";
import vn from "./common/locales/vn";

// 引入全局uview-plus
import uviewPlus, { setConfig } from "@/uni_modules/uview-plus";

// 引入并使用vue-i18n - 修复导入语法
import { createI18n } from "vue-i18n";

// 引入 Pinia
import { createPinia } from "pinia";

// #ifdef VUE3
import { createSSRApp } from "vue";

import { iconList } from "./icon";

// 导入语言工具函数
import { detectDeviceLanguage, getSavedLanguage } from "./utils/language.js";

export function createApp() {
  const app = createSSRApp(App);
  app.use(uviewPlus, () => {
    return {
      options: {
        // 修改config对象的属性
        config: {
          customIcon: {
            family: "iconfont",
            url: "https://at.alicdn.com/t/c/font_4976816_tj1h1j10dq.ttf?t=1759102629851",
          },
          customIcons: {
            ...iconList,
          },
        },
      },
    };
  });

  // 获取语言设置：优先使用已保存的，否则检测设备语言
  let currentLanguage = getSavedLanguage();
  if (!currentLanguage) {
    currentLanguage = detectDeviceLanguage();
    // 保存检测到的语言设置
    uni.setStorageSync("selectedLanguage", currentLanguage);
    console.log(`首次启动，根据设备语言自动设置为: ${currentLanguage}`);
  }

  // 构造i18n对象
  const i18n = createI18n({
    locale: currentLanguage,
    messages: {
      zh: zh,
      en: en,
      vn: vn,
    },
  });

  app.use(i18n);

  // 创建并使用 Pinia
  const pinia = createPinia();
  app.use(pinia);

  return {
    app,
  };
}
// #endif
